<?php

namespace Admin\Controller;

class ReadSeriesController extends AdminController {
	/**
	 * 显示学校读书系列列表
	 * 
	 * 		获取页面参数,获取session中学校数据，查询该学校的学校读书系列列表信息。
	 */
    public function index(){
    	$map['schoolid']=	session('user_auth.schoolid');
        $bookType       =   I('bookType');
        if($bookType!=1&&$bookType!=''){
        	$map['type']=$bookType;
        }
        $map['status']=1;
        $bookList   = $this->lists('BookList',$map);
        
        cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->assign('bookType',C("Book_Type"));//显示读书系列类型列表
        $this->assign('_list', $bookList);
        $this->display();
    }

    /**
     * 添加该学校读书系列
	 *      
     * 		查询读书系列类型列表，显示页面。
	 */
    public function add(){
    	if(IS_POST){
    		$bookSeriesModel	=	D('BookList');
    		$series				=	$bookSeriesModel->create();
    		if(!$series){
    			$this->error($bookSeriesModel->getError());
    		}else{
    			$series['schoolid']		=	session('user_auth.schoolid');
    			$series['schoolname']	=	session('user_auth.schoolname');
    			$series['status']		=	1;
    			$rst=$bookSeriesModel->add($series);
    			if(!$rst){
    				$this->error('读书系列添加失败！');
    			} else {
    				$this->success('读书系列添加成功！请向该系列添加图书！',U('addBook?seriesid='.$rst));
    			}
    		}
    	}else{
			$this->assign('Book_Type',C("Book_Type"));//显示读书系列类型列表
	        $this->display();
    	}
    }
    /**
     * 编辑读书系列
	 * 
     * 		通过系列id查询出读书系列信息，获取读书系列类型列表，显示页面
     */
    public function editSeries(){
    	if(IS_POST){
    		$bookSeriesModel	=	D('BookList');
    		$series				=	$bookSeriesModel->create();
    		if(!$series){
    			$this->error($bookSeriesModel->getError());
    		}else{
    			if($bookSeriesModel->save($series)===false){
    				$this->error('读书系列修改失败！');
    			} else {
    				$this->success('读书系列修改成功！',cookie('__forward__'));
    			}
    		}
    	}else{
	    	$id		=	I('id');
	    	$series	=	M('BookList')->find($id);
	    	$this->assign('series',$series);
	    	$this->assign('Book_Type',C("Book_Type"));//显示读书系列类型列表
	    	$this->display();
    	}
    }
    
    /**
     * 删除读书系列
     *
     */
    public function deleteSeries(){
    	$map['id']		=	I('id');  
    	$data['status']	=	-1;
    	if(!M('BookList')->where($map)->save($data)){
    		$this->error('删除失败！');
    	} else {
    		$this->success('删除成功！',cookie('__forward__'));
    	}
    }
    
    /**
     * 显示读书系列的具体图书列表
	 * 
	 * 		获取页面参数，通过系列id，多表查询该读书系列的图书信息。
	 */
    public function seriesDetail(){
    	$readSeriesModel	=	M('BookListDetail d')
								->join('primary_book_info b ON d.bookid=b.id AND b.status=1','LEFT');//读书系列详细书目左连接了图书表
    	$map['d.seriesid']	=	I('seriesid');
    	
    	//搜索功能
    	$bookname       	=   I('bookname');
    	$map['b.bookname']	=   array('like', '%'.(string)$bookname.'%');
    	$category			=	I('category');
    	if($category!='1'&&$category!=''){
    		$map['b.category']=$category;
    	}
    	
    	$bookOfSeries	=	$this->lists($readSeriesModel,$map,'','b.*,d.id detailid');
    	
        $this->assign('Book_Category',C("Book_Category"));//显示图书分类列表
        $this->assign('seriesid',I('seriesid'));
        $this->assign('_list', $bookOfSeries);
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display();
    }
    
    /**
     * 删除系列内的图书
     */
    public function deleteBookOfSeries(){
    	$SeriesModel	=	M('BookListDetail');
    	$map['id']		=	I('id');
    	if(!$SeriesModel->where($map)->delete()){
    		$this->error('从本系列移除图书失败！');
    	} else {
    		$seriesid=I('seriesid');
    		$this->success('从本系列移除图书成功！',U('seriesDetail','seriesid='.$seriesid));
    	}
    }
    /**
     * 向系列内添加图书
     * 
     * 		获取需要添加到该系列的图书的id，逐一添加到Book_list_detail表中，
     * 		
     * 		根据图书分类，逐一查询得到该类图书信息，回显已存在在该系列的书目。
     */
    public function addBook(){
    	if(IS_POST){
    		$seriesModel	=	M('BookListDetail');
    		$book			=	$seriesModel->create();
    		if($book['bookid']){
    			$seriesModel->startTrans();
	    		$bookid		=	implode( ',' , array_unique($book['bookid']));
	    		foreach ($book['bookid'] as $k => $v) {
	    			$bookOfSeries['bookid']		=	$v;
	    			$bookOfSeries['seriesid']	=	$book['seriesid'];
	    			if(!$seriesModel->where($bookOfSeries)->find()){
	    				//逐一添加到Book_list_detail表中
	    				if (!$seriesModel->add($bookOfSeries)) {
	    					$seriesModel->rollback();
	    					$this->error('修改系列失败！');
	    				}
	    			}
	    		}
	    		//删除图书id未出现在id中的数据
	    		$map['seriesid']=	$book['seriesid'];
	    		$map['bookid']	=	array('not in',$bookid);
	    		if($seriesModel->where($map)->delete()===false){
	    			$seriesModel->rollback();
	    			$this->error('修改系列失败！');
	    		}else {
		    		$seriesModel->commit();
		    		$this->success('修改系列成功！',cookie('__forward__'));
	    		}
	    	}else{
	    		$this->success('该系列内目前没有图书。',cookie('__forward__'));
	    	}
        } else {
        	$bookModel		=	M('BookInfo');
        	$bookCategory	=	C("Book_Category");
        	//逐一查询得到该图书分类的图书信息
        	$bookMap['schoolid']	=	session('user_auth.schoolid');
        	$bookMap['status']		=	1;
        	foreach ($bookCategory as $k => $v) {
        		if($k!=1){
        			$bookOfCategory[$k]['categoryString']=$bookCategory[$k];
        			$bookMap['category']	=	$k;
        			$book	=	$bookModel
								->where($bookMap)
			        			->field('id,bookname')
			        			->select();
        			$bookOfCategory[$k]['book']=$book;
        		}
        	}
        	//回显已存在在该系列的书目
        	$map['seriesid']=	I('seriesid');
        	$seriesModel	=	M('BookListDetail');
        	$bookInSeriesId	=	$seriesModel->where($map)->field('bookid')->select();
        	$bookInSeriesId	=	implode( ',' , array_column($bookInSeriesId,'bookid'));
        	
        	$this->assign('bookid',$bookInSeriesId);
        	$this->assign('book',$bookOfCategory);
            $this->display();
        }
    }
}